﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblUserAccountDAO
    {
        private DBService _db = null;
        public TblUserAccountDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }


        public bool Insert_Update(TblUserAccount user, int mode)
        {
            bool flag = false;
            try
            {
                int result = 0;
                result = _db.ExeSP_NonQuery("sp_TblUserAccount_InsUp",
                    new string[] { "@AccountID", "@Password", "@Email", "@AccountStatus", "@FirstName", "@LastName", "@IsSupperAdmin", "@Address", "@Phone", "@Mode" },
                    new object[] { user.AccountID, user.Password, user.Email, user.AccountStatus, user.FirstName, user.LastName, user.IsSupperAdmin, user.Address, user.Phone, mode },
                    CommandType.StoredProcedure);

                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }


        // Admin Get

        public TblUserAccount GetAdmin(string Email, string PassWord)
        {
            TblUserAccount user = new TblUserAccount();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("TblUserAccount_CheckLogin",
                   new string[] { "@Email", "@Password" },
                   new object[] { Email, PassWord },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.Email = dr["Email"].ToString();
                    user.AccountID = Convert.ToInt32(dr["AccountID"]);
                    user.AccountStatus = Convert.ToInt32(dr["AccountStatus"]);
                    user.Address = dr["Address"].ToString();
                    user.Phone = dr["Phone"].ToString();
                    user.LastName = dr["LastName"].ToString();
                    user.FirstName = dr["FirstName"].ToString();
                    user.Password = dr["Password"].ToString();
                    user.IsSupperAdmin = Convert.ToBoolean(dr["IsSupperAdmin"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }

        // check Email

        public bool CheckExits(string mail, int mode)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_CheckEmail",
                    new string[] { "@Email", "@Mode" },
                    new object[] { mail, mode },
                    CommandType.StoredProcedure);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

    }
}
